<?php
$config_file = parse_ini_file("../../properties/config.ini");
$include_path = "../";
include_once("../util/db_manager.php");
include_once("../util/log_writer.php");
require_once("commentlib.php");
$lw = LogWriter::getInstance("../../properties/log4php-level2.properties");
$error = false;
$msg = "";
try {
	$artifact_type = $_POST["artifactType"];
	$artifact_id = $_POST["artifactId"];
	$page_no = $_POST["pageNo"];
	//start db transaction
	$lw->debug("getting db manager");
	$dbManager = DBManager::getInstance($config_file);
	$lw->debug("getting connection");
	$conn = $dbManager->fetchConnection();
	
	$lw->debug("getting 10 records");
	$query = "SELECT DATE_FORMAT(dt_time, '%W, %b %e, %Y @ %h:%i %p') 'dt',name,location,message FROM comments " .
				"WHERE artifact_type='" . $artifact_type . "' " .
				"AND artifact_id=" . $artifact_id . 
				" ORDER BY comment_id DESC LIMIT " . ($page_no*10 - 10) . ",10";
	$lw->debug(" firing " . $query);
	$result = $dbManager->processQuery($query,$conn);
	$lw->debug(" getting comment list ");
	$comments = Comment::getCommentList($result);
	$lw->debug("getting max page no");
	$query = "SELECT count(1) 'total' FROM comments " .
				"WHERE artifact_type='" . $artifact_type . "' " .
				"AND artifact_id=" . $artifact_id; 
	$lw->debug(" firing " . $query);
	$result = $dbManager->processQuery($query,$conn);
	$lw->debug(" getting total ");
	while ($row = mysql_fetch_array($result)) {
		$total = CEIL(intval($row["total"])/10);
	}
	$lw->debug("closing connection");
	$dbManager->releaseConnection($conn);
	$lw->debug("done");
} catch (Exception $e) {
	$lw->error($e);
	$error = true;
	$msg = $e->getMessage();
} 
	include("comment_loader_message_parser.php");
?>
